package logic

import (
	"context"
	"errors"
	"video-system/model/mysql"

	"video-system/userrpc/internal/svc"
	"video-system/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateStatusLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateStatusLogic {
	return &UpdateStatusLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改状态
func (l *UpdateStatusLogic) UpdateStatus(in *userrpc.UpdateStatusRequest) (*userrpc.UpdateStatusResponse, error) {
	// todo: add your logic here and delete this line

	var findVideo mysql.VideoInfo

	err := findVideo.FindVideoById(mysql.DB, in.VideoId)
	if err != nil {
		return nil, err
	}

	if findVideo.ID == 0 {
		return nil, errors.New("数据不存在")
	}

	findVideo.VideoStatus = in.VideoStatus

	if err := findVideo.UpdateStatus(mysql.DB); err != nil {
		return nil, err
	}

	return &userrpc.UpdateStatusResponse{
		VideoId: int64(findVideo.ID),
	}, nil
}
